from django.contrib import admin
from django.contrib.admin.decorators import register
from django.contrib.admin.options import ModelAdmin
from django.shortcuts import HttpResponseRedirect
from django.urls import reverse
# from django.utils.html import format_html

from learn1.models import HostInfo,cfgFile

# Register your models here.

@admin.register(HostInfo)
class HostInfoAdmin(admin.ModelAdmin):
    # change_form_template = 'admin/hostInfoChange_form.html'

    list_display = ('hostName','HostIp','enable',)
    list_editable = ('enable',)
    
    def has_add_permission(self, request):
        return True
    
    def has_delete_permission(self, request, obj=None):
        return False

    # def response_change(self, request, obj):
    #     print(request.POST)
    #     if "_make-unique" in request.POST:
    #         # matching_names_except_this = self.get_queryset(request).filter(name=obj.name).exclude(pk=obj.id)
    #         # matching_names_except_this.delete()
    #         # obj.is_unique = True
    #         # obj.save()
    #         self.message_user(request, "This villain is now unique")
    #         return HttpResponseRedirect(".")
    #     return super().response_change(request, obj)

    # def has_delete_permission(self, request, obj=None):
    #     return False
        
    # def get_actions(self, request):
    #     actions = super().get_actions(request)
    #     if 'delete_selected' in actions:
    #         del actions['delete_selected']
    #     return actions


    # def actions_html(self, obj):
    #     return format_html('<button class="btn" type="button" onclick="activate_and_send_email({pk})">Activate and send email</button>', pk=obj.pk)

    # actions_html.allow_tags = True
    # actions_html.short_description = "Actions"

    
    

@admin.register(cfgFile)
class cfgFileAdmin(admin.ModelAdmin):
    # change_form_template = 'admin/hostInfoChange_form.html'

    list_display = ('cfgName','cfgFile','enable',)

    actions=['show_status',]

    def show_status(modelAdmin, request, queryset):
        filename_id = queryset.values()[0]['id']
        return HttpResponseRedirect(reverse('learn1:success',args=(filename_id,)))
    show_status.short_description = "测试"

